<?php
namespace Admin\Controller;

class OstateController extends BaseController{
    public function index(){
    	$Ostate = D('Ostate');
    	$this->assign('All',$Ostate->all($this->page($Ostate->total())));
    	$this->display();
    }
    
    public function add(){
    	if (IS_POST){
    		$Ostate = D('Ostate');
    		$object = $Ostate->insert();
    		if (is_numeric($object)){
    			$object>0 ? $this->success(U(CONTROLLER_NAME.'/index'),'订单状态添加成功！') : $this->failed('订单状态添加失败！');
    		}else{
    			$this->failed($object);
    		}
    	}
    	$this->display();
    }
    
    public function update(){
		if (I('get.id')){
			$Ostate = D('Ostate');
			if (IS_POST){
				$object = $Ostate->update();
				is_numeric($object) ? $this->success(U(CONTROLLER_NAME.'/index'),'订单状态修改成功！') : $this->failed($object);
			}
			$object = $Ostate->one();
			if (!$object) $this->failed('不存在此订单状态！');
			$this->assign('One',$object);
			$this->display();
		}else{
			$this->failed('非法操作！');
		}
    }
    
    public function selected(){
		if (I('get.id')){
			$Ostate = D('Ostate');
    		if (!$Ostate->one()) $this->failed('不存在此订单状态！');
    		if (!$Ostate->selected()) $this->failed('设置默认订单状态失败！');
    		$this->success(C('PREV_URL'));
    	}else{
    		$this->failed('非法操作！');
    	}
    }
	
    public function sort(){
    	if (IS_POST){
    		$Ostate = D('Ostate');
    		foreach (I('post.sort') as $key=>$value){
    			if (!is_numeric($value)) continue;
    			$Ostate->sort($key,$value);
    		}
    		$this->success(C('PREV_URL'),'订单状态排序成功！');
    	}
    }
    
    public function delete(){
		if (I('get.id')){
			$Ostate = D('Ostate');
			if (IS_POST) $Ostate->remove() ? $this->success(I('post.prev'),'订单状态删除成功！') : $this->failed('订单状态删除失败！');
			if (!$Ostate->one()) $this->failed('不存在此订单状态！');
    		$this->confirm('您真的要删除这条数据么？');
    	}else{
    		$this->failed('非法操作！');
    	}
    }
}